<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ include file="/common/dateUtil.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<head>


<script>

//////////////////////////////////////////////变量
var rep2_post = {};
rep2_post.debug = true;//调试信息
rep2_post.data = {};//数据对象为空
rep2_post.data.comNoMap = {};//以comNo为key的map，保存企业信息数据
rep2_post.data.depIdMap = {};//以depId为key的map，保存企业信息数据
rep2_post.data.resIdMap = {};//以流水号为key的map，保存回复数据
rep2_post.slctUrl = globalConfig.webPath + "/basicSlctAction/basicSelectData.action";
  


//////////////////////////////////////////////初始化控件
//初始化数据表格
$(function(){
    //初始化表格
    $("#rep2_post_datagrid").datagrid({
        height:"100%",
        toolbar:"#rep2_post_toolbar",//工具条
        pagination:true,//分页工具栏
		pageList:[30,50,100,200],
		pageSize:30,
		singleSelect:true,//单选
		rownumbers:true,//行号
		striped:true,//隔行变色
		fitColumns: true,//填充列
		checkOnSelect: false,//选中则勾选
		selectOnCheck: false,//勾选则选中
    });
});

//接口，初始化控件
rep2_post.initDivs = function(){
    this.initDepDiv();
    this.initPaginationDiv();
    this.initDateDiv();
}

//初始化车组信息
rep2_post.initDepDiv = function(){
    //创建下拉部门树
    Utility.createDepTree("rep2_post_depName");
}

//初始化分页控件点击事件
rep2_post.initPaginationDiv = function(){
    let that = this;
    //选择页面事件定义
    $("#rep2_post_datagrid").datagrid("getPager").pagination({
        onSelectPage: function (pageNumber, pageSize) {
            that.queryAllData(pageNumber);
        }
    });
}

//初始化查岗起始时间，默认为当前
rep2_post.initDateDiv = function(){
    let now = new Date();
    now.setHours(0,0,0,0);
    let start = Utility.dateToString(now);
    $("#rep2_post_StartDate").val(start);

    now.setHours(23,59,59,0)
    let end = Utility.dateToString(now);
    $("#rep2_post_EndDate").val(end);
}


//////////////////////////////////////////////////////////////初始化数据

//接口，初始化数据
rep2_post.initDatas =function(){
    this.initComDatas();
}

//初始化企业数据
rep2_post.initComDatas = function(){
    let that = this;
    // let url = globalConfig.webPath + "/basicSlctAction/basicSelectData.action";
    let param = {};
    param.queryId = "reportV2.getComNoDatas";
    Utility.getAjaxData(rep2_post.slctUrl,JSON.stringify(param),function(data){
        // that.outDebug("企业数据： " , data);
        if(data.success){
            that.formatComDatas(data.data);
            that.outDebug("comNoMap :" , that.data.comNoMap);
            that.outDebug("depIdMap : " , that.data.depIdMap);
        }else{
            DataUtil.showErrorMsg(data.message);
        }
    });
}

//解析企业数据
rep2_post.formatComDatas = function(data){
    let that = this;
    //初始化
    this.data.comNoMap = {};
    this.data.depIdMap = {};
    //开始解析
    if(!DataUtil.isDefined(data)){
        if(data.length > 0){
            $.each(data,function(n,item){
                if(!DataUtil.isDefined(item.comNos) && item.comNos != ""){
                    let list = item.comNos.split(',');
                    if(list.length > 0){
                        //以comNo为key缓存数据
                        for(let i=0 ; i<list.length ; i++){
                            let obj = {};
                            obj.depId = item.depId;
                            obj.depName = item.depName;
                            obj.comNo = list[i];
                            that.data.comNoMap[obj.comNo] = obj;
                        }
                        //以depId为key缓存数据
                        item.list = list;
                        that.data.depIdMap[item.depId] = item;
                    }
                }
            });
        }
    }
}


//////////////////////////////////////////////////////////////加载数据


//查询查岗数据
rep2_post.queryAllData = function(pageNum){
    if(pageNum == 1){
        this.setFirstPage();
    }
    // this.outDebug("开始查询！")
    let that = this;
    let param = this.getQueryParam();
    if(param.parseMsg == ""){
        if(!DataUtil.isDefined(pageNum) &&  pageNum > 0){
            param.pageNumber = pageNum;
            that.outDebug('param : ' , param);  
        }
        let url = globalConfig.webPath + "/basicSlctAction/basicSelectPaginationData.action";
        if (!DataUtil.isDefined(param.depId) && DataUtil.isDefined(param.comNoList)) {
            that.outDebug("当前车组不存在经营许可证号！")
        }else{
            Utility.showLoading();//遮罩
            Utility.getAjaxData(url, JSON.stringify(param), function (data) {
                // that.outDebug("接收到历史查岗数据：", data);
                if (data.success) {
                    if (!DataUtil.isDefined(data.data.rows) && data.data.rows.length > 0) {
                        let param2 = that.getQueryAnswParam(data.data.rows);
                        if (param2.parMsg == "") {
                            that.outDebug("param2 = ", param2);
                            Utility.getAjaxData(rep2_post.slctUrl, JSON.stringify(param2), function (data2) {
                                that.outDebug("查岗回复数据：", data2);
                                if (data2.success) {
                                    if (!DataUtil.isDefined(data2.data)) {
                                        that.formatPostAnswDatas(data2.data);
                                    }
                                }
                                that.outDebug("解析后的查岗数据：", that.data.resIdMap);
                                Utility.hideLoading();//取消遮罩
                                $("#rep2_post_datagrid").datagrid("loadData", data.data);
                            });
                        } else {
                            Utility.hideLoading();//取消遮罩
                            DataUtil.outError(param2.parMsg);
                            $("#rep2_post_datagrid").datagrid("loadData", data.data);
                        }
                    } else {
                        Utility.hideLoading();//取消遮罩
                        $("#rep2_post_datagrid").datagrid("loadData", data.data);
                    }
                } else {
                    Utility.hideLoading();//取消遮罩
                    DataUtil.showErrorMsg(data.message);
                }
            });
        }
        
            // DataUtil.showMsg("开始查询!");
    }else{
        DataUtil.showErrorMsg(param.parseMsg);
    }
}


//获取查询相关参数
rep2_post.getQueryParam = function(){
    let param = {};
    try {
        let formTemp = document.getElementById("rep2_post_slctForm");
        let formData = new FormData(formTemp);
        param = DataUtil.parseFormData2Obj(formData);

        if(!DataUtil.isDefined(param.depId) && param.depId == "0"){
            delete param.depId;
        }

        //查岗命令格式
        param.cmd = '37632';
        param.subCmd = '37633';
        // param.resCmd = '4864';
        // param.resSubCmd = '4865';
        
        //解析depId
        if(!DataUtil.isDefined(param.depId) && param.depId != ""){
            let obj = this.data.depIdMap[param.depId];
            if(!DataUtil.isDefined(obj)){
                param.comNoList = obj.list;
            }
        }

        param.pageNumber = $("#rep2_post_datagrid").datagrid("getPager").data("pagination").options.pageNumber;
	    param.pageSize = $("#rep2_post_datagrid").datagrid("getPager").data("pagination").options.pageSize;
    } catch (error) {
        param.parseMsg += "获取查询参数失败：" + error;
    }
    return param;
}


//获取查询回复内容相关参数
rep2_post.getQueryAnswParam = function(data){
    let param = {parMsg:""};
    if(!DataUtil.isDefined(data)){
        if(data.length > 0){
            let startValue = 0;//开始时间
            let endValue = 0;//结束时间
            let resIdList = [];//流水号
            $.each(data,function(n,item){
                //流水号
                if(!DataUtil.isDefined(item.resId)){
                    resIdList.push(item.resId);
                }
                //时间
                if(!DataUtil.isDefined(item.createDate)){
                    let time = new Date(item.createDate).getTime();
                    //开始时间
                    if(startValue == 0){
                        startValue = time;
                    }else if(time < startValue){
                        //当前时间小于开始时间，则更新开始时间
                        startValue = time;
                    }

                    //结束时间
                    if(endValue == 0){
                        endValue = time;
                    }else if(time > endValue){
                        endValue = time;
                    }
                }
            });//end each 

            //整合参数
            if(startValue > 0){
                param.startDate = Utility.dateToString(new Date(startValue));
            }else{
                param.parMsg += " 获取查岗回复开始时间失败！ ";
            }
            if(endValue > 0){
                endValue = endValue + 24 * 60 * 60 * 1000;
                param.endDate = Utility.dateToString(new Date(endValue));
            }else{
                param.parMsg += " 获取查岗回复结束时间失败！ ";
            }
            if(resIdList.length > 0){
                param.resIdList = resIdList;
            }else{
                param.parMsg += " 获取查岗回复流水号失败！ ";
            }

            param.cmd = '4864';
            param.subCmd = '4865';
            param.queryId = "reportV2.getPostAnswDatas";

            //校验时间
            if(DataUtil.isDefined(param.startDate)){
                param.parMsg += "获取查岗回复-->开始时间不存在！";
            }
            if(DataUtil.isDefined(param.endDate)){
                param.parMsg += "获取查岗回复-->结束时间不存在！";
            }
        }
    }

    return param;
}


//解析回复数据
rep2_post.formatPostAnswDatas = function(data){
    let that = this;
    this.data.resIdMap = {};
    if(data.length > 0){
        $.each(data,function(n,item){
            if(!DataUtil.isDefined(item.resId)){
                let obj = that.data.resIdMap[item.resId];
                if(DataUtil.isDefined(obj)){
                    obj = {list:[],sucList:[]};
                    that.data.resIdMap[item.resId] = obj;
                }

                obj.list.push(item);//所有数据按照时间顺序，最新的插入到最前面
                //成功数据存入到成功列表
                if(item.status == "发送成功"){
                    obj.sucList.push(item);
                }
            }
        });
    }
}

///////////////////////////////////////////////////////////////////数据表格格式化



//数据表格，格式化企业名称
rep2_post.formatComName = function(value,rowData,rowIndex){
    let str = '';
    if(!DataUtil.isDefined(rowData.comNo)){
        let obj = rep2_post.data.comNoMap[rowData.comNo];
        if(!DataUtil.isDefined(obj)){
            str = obj.depName;
        }
    }
    return str;
}

//格式化查岗类型
rep2_post.formatPostType = function(value,rowData,rowIndex){
    let str = "";
    if(value == "1"){
        str = "下级平台";
    }else if(value == "2"){
        str = "平台单一业户";
    }else if(value == '3'){
        str = "平台所有业户";
    }
    return str;
}

//格式化回复数据
rep2_post.formatPostAnsw = function(value,rowData,rowIndex){
    let str = "";
    if(!DataUtil.isDefined(rowData.resId)){
        let obj = rep2_post.data.resIdMap[rowData.resId];
        if(!DataUtil.isDefined(obj)){
            if(obj.sucList.length > 0){
                str = obj.sucList[0].body + ' (' + obj.sucList[0].owner +')';
            }else if(obj.list.length > 0){
                str = obj.list[0].body + ' (' + obj.list[0].owner +') (未成功)';
            }
        }
    }

    return str;
}
//格式化回复时间
rep2_post.formatAnswDate = function(value,rowData,rowIndex){
    let str = "";
    if(!DataUtil.isDefined(rowData.resId)){
        let obj = rep2_post.data.resIdMap[rowData.resId];
        if(!DataUtil.isDefined(obj)){
            if(obj.sucList.length > 0){
                str = obj.sucList[0].createDate;
            }
        }
    }
    return str;
}

//////////////////////////////////////////////////////////////控件事件

//选择车组
rep2_post.doSelectDep = function(){
	InfoWindow.selectDep();
}

//设置车组信息回调方法，用于查找数组
function onDepSelected(depId,depName){
    // dap_dis.outDebug('开始设置车组名称');
	$("#rep2_post_depName").combotree("setValue", depId);
}




/////////////////////////////////////////////方法
rep2_post.outDebug = function(msg,data){
    if(this.debug == true){
        DataUtil.outDebug(msg,data);
    }
}

//设置页面为显示第一页
rep2_post.setFirstPage = function() {
	var opts = $("#rep2_post_datagrid").datagrid("options");
	var pager = $("#rep2_post_datagrid").datagrid("getPager");
	opts.pageNumber = 1;
	pager.pagination("refresh", {
		pageNumber: 1,
	});
}

/////////////////////////////////////////////加载后执行
$(document).ready(function(){
    rep2_post.initDivs();
    rep2_post.initDatas();
});

</script>

</head>

<body style="min-width:1000px;overflow:auto;">
        <div id="rep2_post_toolbar" class="sh_css_basFontSize">
            <form id="rep2_post_slctForm" ><!--action="<%=ApplicationPath%>/basicSlctAction/basicSelectPaginationData.action"-->
                <input type="hidden" name="queryId" value="reportV2.getPostQuesDatas" />
                <table class="TableBlock" style="width:100%;">
                    <tr>
                        <td>
                            <div>
                                <span>车组名称:</span>
                                <select id="rep2_post_depName" name="depId" style="width:170px;"></select>
                                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="rep2_post.doSelectDep();"></a>
                            </div>
                        </td>
                        <td>
                            <div>
                                <span>查岗起止时间：</span>
                                <input type="text" name="startDate" id="rep2_post_StartDate" class="datetimepicker base_inputImpBorder_css"
                                    style="width:140px;" />
                                <span> -- </span>
                                <input type="text" name="endDate" id="rep2_post_EndDate" class="datetimepicker base_inputImpBorder_css"
                                    style="width:140px;" />
                            </div>
                        </td>
                        <td  align="left">
                            <!-- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="rep2_post.queryAllData(1);">查询</a>&nbsp; -->
                            <div class="base_hori_flexStartDiv_css">
                                <div id="adas_lev_btnQuery" class="sh_css_btn" onclick="javascript:rep2_post.queryAllData(1);">
                                    <div class="ui-icon icon-search" style="width:16px;height:16px;"></div>
                                    <span>查询</span>
                                </div>
                                &nbsp;
                                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="rep2_post.reset();">重置</a>&nbsp;
                                <!-- <a id="allAlarmBtnExport" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-excel'">导出</a> -->
                            </div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <table id="rep2_post_datagrid" class="easyui-datagrid">
            <thead>
                <tr>
                    <th data-options="field:'cmdId',formatter:rep2_post.formatComName" width="110">企业名称</th>
                    <th data-options="field:'comNo'" width="110">许可证号</th>
                    <th data-options="field:'createDate'" width="150">查岗时间</th>
                    <th data-options="field:'type',formatter:rep2_post.formatPostType" width="90">查岗类型</th>
                    <th data-options="field:'body'" width="170">查岗内容</th>
                    <th data-options="field:'cmdData',formatter:rep2_post.formatPostAnsw" width="130">回复内容</th>
                    <th data-options="field:'updateDate',formatter:rep2_post.formatAnswDate" width="130">回复时间</th>
                    <th data-options="field:'resId'" width="100">流水号</th>
                </tr>
            </thead>
        </table>
        </body>